 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-13
 */
package com.ce.pms.common.aop.impl;

import org.apache.log4j.Logger;

import com.ce.pms.common.annotation.AssociatedBuildingQuery;
import com.ce.pms.exception.DaclUnsupportedParameterException;
import com.ce.util.CurrentUserThread;

/**
 * @author yandawei
 *
 */
public aspect AssociateBuildingQueryAdvice extends AbstractAssociateBuildingQueryAdvice{

	private Logger log =  Logger.getLogger(AssociateBuildingQueryAdvice.class);

	pointcut sessionCreateQuery(String sql):execution(* createQuery(String,..)) && args(sql);

	Object around(String sql , AssociatedBuildingQuery associatedBuildingQuery): sessionCreateQuery(sql) && cflowbelowAssociatedBuildingQuery(associatedBuildingQuery){
		Long userId = CurrentUserThread.get().getUserId();
		log.info("===DACL===");
		String alias = associatedBuildingQuery.alias();

		String beginStr = associatedBuildingQuery.beginStr();

		String endStr = associatedBuildingQuery.endStr();

		String insertAfterPoint = associatedBuildingQuery.insertAfterPoint();

		String property4Building = associatedBuildingQuery.property4Building();

		int startPos = sql.indexOf(insertAfterPoint);
		if(startPos == -1){
			throw new DaclUnsupportedParameterException("没找到指定参数"+insertAfterPoint);
		}
		int tempLength = startPos+insertAfterPoint.length();
		String begin = sql.substring(0,tempLength);
		String end = sql.substring(tempLength);
		//所插入的逻辑
		StringBuffer insertQuery = new StringBuffer();
		insertQuery.append(beginStr)
			       .append(alias).append(".").append(property4Building).append(" in (select userRight.resourceId from SecUserRelationRight userRight where (userRight.typeCode='002' and userRight.secUser.userId = ").append(userId).append(") )")
			       .append(endStr);
		sql = begin+insertQuery+end;
		return proceed(sql,associatedBuildingQuery);
	}
}
